package gps.monitor.jt809server.service;

import gps.monitor.common.core.network.ISocketClient;
import gps.monitor.common.core.network.NetworkException;
import gps.monitor.common.tools.network.SimpleNettyConnectConfig;
import gps.monitor.common.tools.network.SocketConnectorFactory;
import gps.monitor.jt809server.network.netty.SimpleJt809NettyEncoder;

/**
 *
 * @author cyq
 * @date 2021/11/4
 * @desc talk is cheep,show me the code
 */
public class DownConnectService {

    private ISocketClient client;

    public boolean connect(String ip, int port) {

        if (client != null) return true;

        try {
            client = SocketConnectorFactory.createOneClient(SimpleNettyConnectConfig.clientConfig(ip,port),
                    new SimpleJt809NettyEncoder());
            client.connet();
        } catch (NetworkException e) {
            e.printStackTrace();
        }

        return true;
    }

    public void sendMsg(String data) {
        try {
            client.write(data);
        } catch (NetworkException e) {
            e.printStackTrace();
        }
    }
}
